package com.c1.client.dialog.show;

import com.c1.client.Frame;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

/**
 * @author zhangjiangnan
 */
@Slf4j
public class ShowDialog extends JDialog implements ActionListener {
    private static volatile ShowDialog me;

    public static ShowDialog me() {
        if (me == null) {
            synchronized (ShowDialog.class) {
                if (me == null) {
                    me = new ShowDialog();
                }
            }
        }
        return me;
    }

    private static final int FONT_SIZE = 20;
    private static final int FONT_STYLE = Font.PLAIN;
    private static final String FONT_NAME = "Serif";

    private static final int WIDTH = Frame.WIDTH * 5 / 6;
    private static final int HEIGHT = Frame.HEIGHT * 5 / 6;

    private JTextArea textArea;

    protected ShowDialog() {
        super();
        setModal(true);
        initTextArea();
        add(new JScrollPane(textArea), BorderLayout.CENTER);

        JPanel queryPanel = new JPanel(new BorderLayout());
        queryPanel.add(getCommandArea(), BorderLayout.EAST);
        add(queryPanel, BorderLayout.SOUTH);

        setFocusable(true);

        setSize(WIDTH, HEIGHT);
        setResizable(false);

        setLocationRelativeTo(null);
    }

    private JPanel getCommandArea() {
        JPanel commandArea = new JPanel();
        JButton cancelButton = new JButton("关闭(ALT+C)");
        cancelButton.addActionListener(this);
        cancelButton.setMnemonic(KeyEvent.VK_C);
        commandArea.add(cancelButton);
        return commandArea;
    }

    private void initTextArea() {
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
    }

    public void setTextArea(String content) {
        textArea.setText(content);
        textArea.setCaretPosition(0);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setVisible(false);
    }
}